package edu.tsinghua.eea.powermanagement.gui;

import java.net.MalformedURLException;

import edu.tsinghua.eea.powermanagement.R;
import edu.tsinghua.eea.powermanagement.control.XmlLoader;
import edu.tsinghua.eea.powermanagement.data.PUData;
import edu.tsinghua.eea.powermanagement.plot.BarCompareView;
import edu.tsinghua.eea.powermanagement.plot.PlotView;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class OverviewActivity extends Activity {

	private PUData mPDataNow;
	private PUData mPDataPrev;
	private PlotView mPlotView;
	private BarCompareView mBarCompareView;
	private TextView mMessage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.overview);

		// initial power data and set PlotView's data source to mPData.
		mPDataNow = new PUData();
		mPDataNow.generateDailyTotalPU(144);
		mPDataPrev = new PUData();
		mPDataPrev.generateDailyTotalPU(144);

		// initial widgets.
		mPlotView = (PlotView) findViewById(R.id.plot_view);
		mBarCompareView = (BarCompareView) findViewById(R.id.bar_compare_view);
		mMessage = (TextView) findViewById(R.id.message_ov);
		
		// set widgets' data.
//		mPlotView.setPUData(mPDataNow);
//		mBarCompareView.setData(BarCompareView.CURRENT_DATA_INDEX,
//				mPDataNow.getEnergyCostInPeriods());
//		mBarCompareView.setData(BarCompareView.PREV_DATA_INDEX,
//				mPDataPrev.getEnergyCostInPeriods());
		// mBarCompareView.invalidate();
// http://59.66.193.26:8080/PowerManagementServer/xml?un=ywx217&pwd=ddsdd&type=0&pid=0&date=2011-05-20
		String url = getURLHead() + "&type=0&pid=0&date=2011-05-20";
		mMessage.setText("Start to load XML\n" + url + "\n");
		XmlLoader xmlLoader = new XmlLoader(this, mMessage,
				mPlotView, mBarCompareView);
		xmlLoader.execute(url);
	}
	
	private String getURLHead(){
		SharedPreferences pref = getSharedPreferences("empreference", MODE_PRIVATE);
		String serv = pref.getString("serverurl", "");
		String un = pref.getString("username", "");
		String pwd = pref.getString("password", "");
		return String.format("%sxml?un=%s&pwd=%s", serv, un, pwd);
	}
}
